package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.FranchiserFundBiz;
import com.coolbi.onsupai.core.entity.FranchiserFund;
import com.coolbi.security.web.action.SessionAction;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * @Description 经销商账户
 * @author for2cold
 * @codeDate 2012-7-23 上午11:50:22
 * 
 */
public class FranchiserFundAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private FranchiserFund franchiserFund;
	private List<FranchiserFund> franchiserFundList;
	@Autowired
	private FranchiserFundBiz franchiserFundBiz;
	private String searchType;
	private String keyWord;

	/**
	 * 根据关键字查询
	 * @return
	 */
	public String findByKeyWord() {
		try {
			pager.setRecordCount(franchiserFundBiz.countBykeyWord(searchType, keyWord));
			franchiserFundList = franchiserFundBiz.findByKeyWord(searchType, keyWord, (pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 添加经销商账户
	 * @return
	 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "franchiserFund.accountType", message = "账户类型必填"), 
			@RequiredStringValidator(fieldName = "franchiserFund.accountNo", message = "账户号码必填"), 
			@RequiredStringValidator(fieldName = "franchiserFund.owner", message = "账户所属必填")
	})
	public String save() {
		try {
			franchiserFundBiz.save(franchiserFund);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 删除经销商账户
	 * @return
	 */
	public String delete() {
		try {
			franchiserFundBiz.delete(franchiserFund.getId());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 更新经销商账户信息
	 * @return
	 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "franchiserFund.accountType", message = "账户类型必填"), 
			@RequiredStringValidator(fieldName = "franchiserFund.accountNo", message = "账户号码必填"), 
			@RequiredStringValidator(fieldName = "franchiserFund.owner", message = "账户所属必填")
	})
	public String update() {
		try {
			franchiserFundBiz.update(franchiserFund);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 修改经销商账户状态
	 * @return
	 */
	public String updateStatus() {
		try {
			franchiserFundBiz.updateStatus(franchiserFund.getId(), franchiserFund.getAccountStatus());
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 根据ID获取经销商账户信息
	 * @return
	 */
	public String findById() {
		try {
			franchiserFund = franchiserFundBiz.get(franchiserFund.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 分页查询所有经销商账户
	 * @return
	 */
	public String listByPage() {
		try {
			pager.setRecordCount(franchiserFundBiz.count());
			franchiserFundList = franchiserFundBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 获取所有经销商账户信息,返回Json数据
	 * @return
	 */
	public String findAllFranchiserFund() {
		String json = null;
		try {
			franchiserFundList = franchiserFundBiz.list();
			json = JsonUtil.toJsonByArray(franchiserFundList);
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
		return ajax(json);
	}
	
	/**
	 * 检测账号是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkAccountNo() {
		boolean flag = false;
		try {
			if (null == franchiserFund.getId()) {
				flag = franchiserFundBiz.checkAccountNo(franchiserFund.getAccountNo());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
		return ajax(String.valueOf(!flag));
	}
	
	public FranchiserFund getFranchiserFund() {
		return franchiserFund;
	}

	public void setFranchiserFund(FranchiserFund franchiserFund) {
		this.franchiserFund = franchiserFund;
	}

	public List<FranchiserFund> getFranchiserFundList() {
		return franchiserFundList;
	}

	public void setFranchiserFundList(List<FranchiserFund> franchiserFundList) {
		this.franchiserFundList = franchiserFundList;
	}

	public void setFranchiserFundBiz(FranchiserFundBiz franchiserFundBiz) {
		this.franchiserFundBiz = franchiserFundBiz;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
}
